
/**
 * @file verGraficas.java
 * @brief muestra la gráfica selecionada.
 *
 * @author José Ángel Carvajal Soto
 * @version 1.0
 *
 * Created on 04-may-2011, 21:00:39
 */

package interfaces;

import clinica.ClinicaView;
import clinica.Interfaces;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.text.ParseException;
import org.jdesktop.application.Action;
import Controlador.*;
import clinica.Interfaz;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JComboBox;
import javax.swing.JPanel;


/**
 * @file verGraficas.java
 * @brief muestra la gráfica selecionada.
 *
 * @author José Ángel Carvajal Soto
 * @version 1.0
 *
 * Created on 04-may-2011, 21:00:39
 */
public class selectorListado extends javax.swing.JPanel {

    private ClinicaView app;
    
    /** Creates new form buscarCita */
    public selectorListado(ClinicaView a) {
        app = a;
        initComponents();
        textField1.setVisible(false);
        jLabel4.setVisible(false);
        textField1.setVisible(false);
        jDateChooser1.setVisible(false);
        jDateChooser2.setVisible(false);
        jLabel1.setVisible(false);
        jLabel2.setVisible(false);

    }

    public void rellenaDatos() {
        jDateChooser1.setDate(Calendar.getInstance().getTime());
        jDateChooser2.setDate(Calendar.getInstance().getTime());

    }



   

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jDateChooser2 = new com.toedter.calendar.JDateChooser();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        textField1 = new java.awt.TextField();
        jPanel6 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(780, 472));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(780, 472));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(selectorListado.class);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel1.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, Interfaz.setFontSize())); // NOI18N
        jPanel1.setFont(Interfaz.setFontSize());
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(680, 372));

        jPanel3.setFont(resourceMap.getFont("jPanel3.font")); // NOI18N
        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setPreferredSize(new java.awt.Dimension(680, 372));

        jLabel1.setFont(Interfaz.setFontSize());
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(Interfaz.setFontSize());
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(Interfaz.setFontSize());
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jDateChooser1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jDateChooser1.setFont(Interfaz.setFontSize());
        jDateChooser1.setName("jDateChooser1"); // NOI18N
        jDateChooser1.setPreferredSize(new java.awt.Dimension(255, 34));

        jDateChooser2.setFont(Interfaz.setFontSize());
        jDateChooser2.setName("jDateChooser2"); // NOI18N
        jDateChooser2.setPreferredSize(new java.awt.Dimension(255, 34));

        jComboBox1.setFont(Interfaz.setFontSize());
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Seleciona Listado>", "Citas por médico", "Citas por paciente", "Farmaco más distribuido", "Farmaco más pedido" }));
        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(selectorListado.class, this);
        jComboBox1.setAction(actionMap.get("selectedChange")); // NOI18N
        jComboBox1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jComboBox1.setName("selecionaGrafica"); // NOI18N
        jComboBox1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jComboBox1MouseClicked(evt);
            }
        });

        jLabel4.setFont(Interfaz.setFontSize());
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        textField1.setFont(Interfaz.setFontSize());
        textField1.setName("dato"); // NOI18N
        textField1.setText(resourceMap.getString("dato.text")); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(31, 31, 31)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jDateChooser2, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)
                    .addComponent(jDateChooser1, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)
                    .addComponent(textField1, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)
                    .addComponent(jComboBox1, 0, 558, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(jLabel1))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(28, 28, 28)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jDateChooser2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(54, 54, 54))
        );

        jPanel6.setName("jPanel6"); // NOI18N
        jPanel6.setPreferredSize(new java.awt.Dimension(680, 51));

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 716, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 27, Short.MAX_VALUE)
        );

        jPanel7.setName("jPanel7"); // NOI18N
        jPanel7.setPreferredSize(new java.awt.Dimension(680, 42));

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 716, Short.MAX_VALUE)
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 18, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 716, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 716, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 716, Short.MAX_VALUE))
                .addGap(16, 16, 16))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, 18, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
                .addContainerGap())
        );

        jButton1.setAction(actionMap.get("aceptar")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setAction(actionMap.get("cancelar")); // NOI18N
        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 756, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
      
    }

    @Action
    public void cancelar() {
        app.volver(Interfaces.PRINCIPAL_GESTOR);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jComboBox1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jComboBox1MouseClicked
        // TODO add your handling code here:

        textField1.setVisible(false);
        jLabel4.setVisible(false);
        textField1.setVisible(false);
        jDateChooser1.setVisible(false);
        jDateChooser2.setVisible(false);
        jLabel1.setVisible(false);
        jLabel2.setVisible(false);
    }//GEN-LAST:event_jComboBox1MouseClicked

    @Action
    public void aceptar() throws SQLException, IOException, ClassNotFoundException, NoSuchAlgorithmException, ParseException {

        ArrayList<String> error=new ArrayList<String>();
        ArrayList<ArrayList<String>> panel=null;
        Integer num;
        String cmp1="error",cmp2="error";

        if(_viejoValor.equals("Citas por médico")){

            try{
                num=Integer.parseInt(textField1.getText());
                //System.out.println(textField1.getText());
                panel = Controlador.ListadoMedico(jDateChooser1.getDate(), jDateChooser2.getDate(), num, error);
                cmp1="Médico";
                cmp2="Citas";
            }
            catch(Exception ex){
                
                error.add("Número introducido no válido");
            }
           
           
       }else if(_viejoValor.equals("Citas por paciente")){


            try{
                num=Integer.parseInt(textField1.getText());
                panel = Controlador.ListadoPaciente(jDateChooser1.getDate(), jDateChooser2.getDate(), num, error);
                cmp1="Paciente";
                cmp2="Citas";
            }
            catch(Exception ex){
                
                error.add("Número introducido no válido");
            }
           

       }else if(_viejoValor.equals("Farmaco más distribuido")){
            try{
                num=Integer.parseInt(textField1.getText());
                panel = Controlador.ListadoFarmaco(jDateChooser1.getDate(), jDateChooser2.getDate(), num, error);

                cmp1="Farmaco";
                cmp2="Núm. Recetas";
            }
            catch(Exception ex){
                
                error.add("Número introducido no válido");
            }
  

       }else if( _viejoValor.equals("Farmaco más pedido")){
            try{
                num=Integer.parseInt(textField1.getText());
                panel = Controlador.ListadoLote(jDateChooser1.getDate(), jDateChooser2.getDate(), num, error);

                cmp1="Farmaco";
                cmp2="Lotes";
            }
            catch(Exception ex){
                
                error.add("Número introducido no válido");
            }

       }
        if (panel==null){
            
            error.add("No hay datos que mostrar");
        }
        if (error.isEmpty())

            app.abrirVerListados(panel,_viejoValor,cmp1, cmp2, Interfaces.PRINCIPAL_GESTOR);

        else
            app.abrirMensajeError(error.get(0), Interfaces.SELECTOR_LISTADOS);
            

        

    }

    @Action
    public void selectedChange(java.awt.event.ActionEvent evt) {
        JComboBox cb = (JComboBox)evt.getSource();

        _viejoValor = cb.getSelectedItem().toString();

         if (_viejoValor.equals("Citas por médico")||_viejoValor.equals("Citas por paciente")||_viejoValor.equals("Farmaco más distribuido")||_viejoValor.equals("Farmaco más pedido")){
            textField1.setVisible(true);
            jDateChooser1.setVisible(true);
            jDateChooser2.setVisible(true);
            jLabel1.setVisible(true);
            jLabel2.setVisible(true);
            jLabel4.setVisible(true);
            jLabel4.setText("Número de columnas:");
        } else{

            textField1.setVisible(false);
            jLabel4.setVisible(false);
            textField1.setVisible(false);
            jDateChooser1.setVisible(false);
            jDateChooser2.setVisible(false);
            jLabel1.setVisible(false);
            jLabel2.setVisible(false);

        }

           
    }

  
    private String _viejoValor;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private com.toedter.calendar.JDateChooser jDateChooser2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private java.awt.TextField textField1;
    // End of variables declaration//GEN-END:variables

}
